﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Reflection;
using System.Diagnostics;

namespace NCP.Recruiting.Web.Framework.Controllers
{
    public class FormValueRequiredAttribute : ActionMethodSelectorAttribute
    {
        private readonly string[] _submitButtonNames;

        public FormValueRequiredAttribute(params string[] submitButtonNames)
        {
            this._submitButtonNames = submitButtonNames;
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            foreach (string buttonName in _submitButtonNames)
            {
                try
                {
                    if (!String.IsNullOrEmpty(controllerContext.HttpContext.Request.Form[buttonName]))
                        return true;
                }
                catch (Exception exc)
                {
                    Debug.WriteLine(exc.Message);
                }
            }
            return false;
        }
    }
}
